<?php

/* Load widget with widget_init function */
add_action( 'widgets_init', 'slideshow_widget' );

/* Register widget */
function slideshow_widget()
{
	register_widget( 'slideshow_widget' );
}

/* Handler class for all widget params */
class slideshow_widget extends WP_Widget {

	/* Widget setup. */
	function slideshow_widget()
	{
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'slideshow_widget', 'description' => __('An image gallery widget.', 'slideshow_widget') );
		/* Widget control settings. */
		$control_ops = array( 'width' => 350, 'height' => 400, 'id_base' => 'slideshow_widget' );
		/* Create the widget. */
		$this->WP_Widget( 'slideshow_widget', 'Slideshow Widget', $widget_ops, $control_ops );
	}

	/** Widget display template */
	function widget( $args, $instance )
	{
		extract( $args );

		/* Get widget settings */
		$title = apply_filters('widget_title', $instance['title'] );
		if ( isset($instance['id']) ) $id = $instance['id'];
		$images = $instance['images'];
		$image_width = $instance['image_width'];
		$image_height = $instance['image_height'];
		$slideshow_transitions = $instance['slideshow_transitions'];
		
		$random_ident = rand(0,999999);
		$random_letter = rand(97, 122);
		$random_ident .= chr($random_letter);
		
		if ( $image_width == '' ) $image_width = 280;
		if ( $image_height == '' ) $image_height = 200;
		
		/* Widget themplate */
		echo $before_widget;
		
	   	if ( $title ) echo $before_title . $title . $after_title;
		$urls = explode("\n", $images);
		
		echo '
			<script type="text/javascript">
				jQuery(window).load(function() {
					jQuery(document).ready(function($) {
						jQuery(".sidebar_slideshow-'.$random_ident.'").nivoSlider({
									controlNav:false, 
									directionNavHide:false, 
									slices:10, 
									boxCols:4, 
									boxRows:4, 
									animSpeed:500, 
									pauseTime:4000,
									manualAdvance:true,
									effect:\''.$slideshow_transitions.'\'
						});
					});
				});
			</script>
		';

		echo '<div class="sidebar_slideshow-'.$random_ident.'" style="overflow: hidden; height: '.$image_height.'px; width: '.$image_width.'px;">';
	
		foreach($urls as $url):
		
			$thumbnail_size = 'w='.$image_width.'&amp;h='.$image_height;
			
			$url = trim($url);
			$url = str_replace(' ', '', $url);
				
			$image_source = get_template_directory_uri().'/includes/timthumb.php?src='.$url.'&amp;'.$thumbnail_size.'&amp;zc=1&amp;q=100';
			echo '<a href="'.$url.'" rel="prettyPhoto[slideshow_widget_'.$random_ident.']">';
				echo '<img src="'.$image_source.'" alt="slide" />';
			echo '</a>';
		
		endforeach;
		echo '</div>';
		
		echo $after_widget;
	}

	/* Update widget settings. */
	function update( $new_instance, $old_instance )
	{
		$instance = $old_instance;

		/* Create widget settings instances. */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['images'] = $new_instance['images'];
		$instance['image_width'] = $new_instance['image_width'];
		$instance['image_height'] = $new_instance['image_height'];
		$instance['slideshow_transitions'] = $new_instance['slideshow_transitions'];
		return $instance;
	}

	/* Admin panel form  */
	function form( $instance )
	{
		$instance = wp_parse_args( (array) $instance );

		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('title'); echo '">'; echo 'Widget Title'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('title'); echo '" name="'; echo $this->get_field_name('title'); echo '" value="'; if( isset($instance['title']) ) echo $instance['title']; echo '" />';
		echo '</p>';
        
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('images'); echo '">'; echo 'Image URLs:'; echo '</label>';
			echo '<br />';
			echo '<textarea id="'; echo $this->get_field_id('images'); echo '" name="'; echo $this->get_field_name('images'); echo '">'; if( isset($instance['images']) ) echo $instance['images']; echo '</textarea>';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('image_width'); echo '">'; echo 'Image Width'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('image_width'); echo '" name="'; echo $this->get_field_name('image_width'); echo '" value="'; if( isset($instance['image_width']) ) echo $instance['image_width']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('image_height'); echo '">'; echo 'Image Height'; echo '</label>';
			echo '<br />';
			echo '<input id="'; echo $this->get_field_id('image_height'); echo '" name="'; echo $this->get_field_name('image_height'); echo '" value="'; if( isset($instance['image_height']) ) echo $instance['image_height']; echo '" />';
		echo '</p>';
		
		echo '<p>';
			echo '<label for="'; echo $this->get_field_id('slideshow_transitions'); echo '">'; echo 'Transitions'; echo '</label>';
			echo '<br />';
			
			echo '<select id="'; echo $this->get_field_id('slideshow_transitions'); echo '" name="'; echo $this->get_field_name('slideshow_transitions'); echo '">';
				echo '<option value="sliceDown"';           if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'sliceDown')          echo ' selected="selected"'; echo '>SliceDown</option>';
				echo '<option value="sliceDownLeft"';       if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'sliceDownLeft')      echo ' selected="selected"'; echo '>SliceDownLeft</option>';
				echo '<option value="sliceUp"';            if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'sliceUp')            echo ' selected="selected"'; echo '>SliceUp</option>';
				echo '<option value="sliceUpLeft"';        if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'sliceUpLeft')        echo ' selected="selected"'; echo '>SliceUpLeft</option>';
				echo '<option value="sliceUpDown"';        if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'sliceUpDown')        echo ' selected="selected"'; echo '>SliceUpDown</option>';
				echo '<option value="sliceUpDownLeft"';    if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'sliceUpDownLeft')    echo ' selected="selected"'; echo '>SliceUpDownLeft</option>';
				echo '<option value="fold"';               if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'fold')               echo ' selected="selected"'; echo '>Fold</option>';
				echo '<option value="fade"';               if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'fade')               echo ' selected="selected"'; echo '>Fade</option>';
				echo '<option value="random"';             if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'random')             echo ' selected="selected"'; echo '>Random</option>';
				echo '<option value="slideInRight"';       if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'slideInRight')       echo ' selected="selected"'; echo '>SlideInRight</option>';
				echo '<option value="slideInLeft"';        if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'slideInLeft')        echo ' selected="selected"'; echo '>SlideInLeft</option>';
				echo '<option value="boxRandom"';          if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'boxRandom')          echo ' selected="selected"'; echo '>BoxRandom</option>';
				echo '<option value="boxRain"';            if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'boxRain')            echo ' selected="selected"'; echo '>BoxRain</option>';
				echo '<option value="boxRainReverse"';     if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'boxRainReverse')     echo ' selected="selected"'; echo '>BoxRainReverse</option>';
				echo '<option value="boxRainGrow"';        if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'boxRainGrow')        echo ' selected="selected"'; echo '>BoxRainGrow</option>';
				echo '<option value="boxRainGrowReverse"'; if(isset($instance['slideshow_transitions'])) if ($instance['slideshow_transitions'] == 'boxRainGrowReverse') echo ' selected="selected"'; echo '>BoxRainGrowReverse</option>';
			echo '</select>';
			
		echo '</p>';

	}
}

?>